<template>
  <div>
    <div>
      
      <img src="../../../public/返回顶部.png" alt="无" class="back_top" @click="backTop" v-show="isShow">
        <!-- <span>返回顶部</span> -->
      <div id="header">
        <img src="../../../public/header.png" alt="智能问答" />
      </div>
      <div id="now_time"></div>
      <div id="robot">
        <div class="robot_top">
          <i><img src="../../../public/头像.png" alt="无" /></i>
          <span>您好，我是新冠肺炎智能问答机器人：</span>
        </div>
        <div class="robot_bottom">
          我学习了百余篇新冠肺炎相关论文、权威指南、政府报道、专家访谈等。我来为您解答新冠肺炎相关的健康问题。
        </div>
      </div>
      <div id="ask">
        <h2>猜你想问</h2>
        <div class="ask1">
          <span @click="show_input">居家如何进行消毒</span>
        </div>
        <div class="ask2">
          <span @click="show_input">用过的口罩如何处理</span>
        </div>
        <div class="ask3">
          <span @click="show_input">就诊有哪些注意事项</span>
        </div>
        <div class="ask4">
          <span @click="show_input">企业复工复产后，员工健康防护该注意哪些问题</span>
        </div>
        <div class="ask5">
          <span @click="show_input">企业复工后，乘坐公共交通工具如何做好手卫生</span>
        </div>
      </div>
      <el-button id="ask_btn" v-show="btn_" @click="show_input"
        >输入问题</el-button>
      <div id="bottom_input" v-show="input_">
        <div id="all_container">
          <div id="input_container">
                <el-autocomplete
                size='large'
                  class="inline-input"
                  v-model="state1"
                  :fetch-suggestions="querySearch"
                  placeholder="请输入内容"
                  @select="handleSelect"
                  ref="text_input"
                  clearable
                  ></el-autocomplete>
          </div>
          <el-button class="right" type="primary" @click="sign_out">
            <span>退出</span>
          </el-button>
          <a href="#dd">
            <el-button class="left" type="primary" @click="query_input()" >
            <span>查询</span>
          </el-button>
          </a>
        </div>
      </div>
      <div model="obj" v-for="(val,index) in obj" :key="index">
          <div id="question_box" v-show="show_answer">
          <div id="question"  model="obj">
              <p>{{val.input}}</p>
          </div>
      </div>

      <div id="robot_answer" v-show="show_answer">
        <div class="robot_top">
          <i><img src="../../../public/头像.png" alt="" /></i>
          <span v-if="val.value">以下是为您查询的结果：</span>
          <span v-else>抱歉，没有找到答案：</span>
        </div>
        <div class="robot_bottom">
          <h2 v-if="val.value">{{val.value}}</h2>
          <h2 v-else></h2>
          <p v-if="val.address">{{val.address}}</p>
          <p v-else>可能我还不够聪明，没有能力回答这个问题。如果你愿意，可以试试搜索，点击 <a href="http://www.baidu.com" target="_blank">百度一下</a></p>
        </div>
      </div>
      </div>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <!-- <a id="dd"></a> -->
    </div>
    <a id="dd"></a>
  </div>
</template>

<style scoped>
* {
  padding: 0;
  margin: 0;
}

a{
    color: aqua;
    font-size: 30px;
}
#header img {
  width: 100%;
  display: block;
}
#now_time {
  text-align: center;
  color: #8d8d8d;
  padding: 10px 0;
  font-size: 15px;
}
#robot {
  background-color: #ededed;
  height: 100px;
  padding: 10px;
  border-radius: 10px;
}
#robot i {
  float: left;
  margin-left: 15px;
  margin-right: 10px;
}
#robot span {
  float: left;
  height: 33px;
  line-height: 33px;
  color: #999;
}
#robot i img {
  width: 30px;
  height: 30px;
}
#robot .robot_top {
  height: 40px;
  border-bottom: 1px solid #fff;
}
#robot .robot_bottom {
  padding-left: 20px;
  line-height: 50px;
  height: 50px;
}
#ask {
  margin-top: 20px;
  padding: 15px;
  background-color: #ededed;
  border-radius: 10px;
}
#ask div {
  height: 50px;
  line-height: 50px;
}
#ask .ask1,
.ask2,
.ask3,
.ask4 {
  border-bottom: 1px solid #fff;
}
#ask_btn {
  width: 125px;
  height: 45px;
  text-align: center;
  line-height: 45px;
  background-color: rgb(255, 139, 159);
  color: #fff;
  font-size: 20px;
  font-weight: 800;
  border-radius: 20px;
  position: fixed;
  bottom: 0;
  left: 50%;
}
#bottom_input {
  width: 87%;
  height: 60px;
  bottom: 0;
  padding: 10px;
  background-color: #ededed;
  position: fixed;
  border-radius: 10px;
}
.el-autocomplete {
    width: 100%;
}
#input_container {
  height: 40px;
  width: 90%;
  float: left;
}
#bottom_input .left,
.right {
    position: absolute;
  padding: 5px;
  font-size: 15px;
  border-radius: 5px;
  background-color: #a5a5a5;
  /* color: #fff; */
}
#bottom_input .left{
    top: 16px;
    right: 75px;
}
#bottom_input .right{
    top: 16px;
    right: 20px;
}
#bottom_input .left:hover {
  background-color: #757575;
}
#bottom_input .right:hover {
  background-color: #757575;
}
#all_container {
  height: 40px;
}
#question_box{
    width: 100%;
    height: 55px;
    margin-top: 20px;
    animation: question_box .8s;
}
@keyframes question_box {
            0% {
                opacity: 0;
                transform: translateY(40px);
            }
            100% {
                opacity: 1;
                transform: translateY(0) scale(.9);
            }
        }
#question{
    height: 55px;
    line-height: 55px;
    background: #3b52c0;
    color: #fff;
    border-radius: 10px 0 10px 10px;
    font-size: 17px;
    padding-right: 10px;
    padding-left: 10px;
    float: right;
}
#question p{
    height: 55px;
    line-height: 55px;
}
#robot_answer {
  background-color: #ededed;
  padding: 10px;
  border-radius: 10px;
  margin-top: 20px;
  animation: robot_answer .8s;
}
@keyframes robot_answer {
            0% {
                opacity: 0;
                transform: translateY(20px);
            }
            100% {
                opacity: 1;
                transform: translateY(0) scale(.9);
            }
        }
#robot_answer i {
  float: left;
  margin-left: 15px;
  margin-right: 10px;
}
#robot_answer span {
  float: left;
  height: 33px;
  line-height: 33px;
  color: #999;
}
#robot_answer i img {
  width: 30px;
  height: 30px;
}
#robot_answer .robot_top {
  height: 40px;
  border-bottom: 1px solid #fff;
}
#robot_answer .robot_bottom {
  padding-left: 20px;
  line-height: 50px;
  height: 50px;
}
#robot_answer .robot_bottom h2{
    margin-top: 15px;
        font-size: 17px;
        color: #222;
        font-weight: bold;
        text-align: justify;
        line-height: 22px;
}
.back_top{
  position: fixed;
  width: 60px;
  height: 60px;
  bottom: 9%;
  right: 2%;
  text-align: center;
  cursor: pointer;
  transition:width 2s;
  transition:height 2s;
  transition-duration: 1s;
  transition-property:all;
}
.back_top:hover{
  height: 100px;
  width: 100px;
}

</style>

<script>
import moment from "moment";
export default {
  data() {
    return {
      isShow: false,
      times: "",
      dialog_ask: false,
      title: "",
      count: 0,
      input: "",
      input_: false,
      btn_: true,
      restaurants: [],
        state1: '',
        state2: '',
        show_answer: false,
        obj:[],
        item:{},
        txt_input:'',
        obj2:{},
    };
  },
  created(){
    this.getClock();
  },
  mounted() {
      this.restaurants = this.loadAll();
      this.listenerFunction();
    },
  methods: {
    getClock(){
      setInterval(()=>{
        this.getTime()
      },1000)
    },

    getTime() {
        let time = moment(new Date()).format("YYYY年MM月DD日 HH:mm:ss");
        document.getElementById("now_time").innerHTML = time;
    },
    show_input() {
      this.input_ = true;
      this.btn_ = false;
    },
    query_input() {
        this.show_answer = true;
        // this.obj.title = this.item.value;
        // this.obj.passage = this.item.address;
        this.txt_input = this.$refs.text_input.value;
        console.log(this.txt_input.length,'555');

        let res = this.restaurants.filter((item)=>{
            if(item.value.indexOf(this.txt_input) == 0) {
                return item
            }
        })

        var res2 = res.filter((item)=>{
            console.log(item,'=====');
            return item != undefined
        })
        if (this.txt_input.length == 0) {
            this.obj.push({value:'',address:'',input:this.txt_input});
        }
        else if(res2.length > 0) {
            var val = res2[0];
            this.obj.push({value:val.value,address:val.address,input:this.txt_input});        
        } else {
            this.obj.push({value:'',address:'',input:this.txt_input});
        }
        this.obj2 = this.obj[0];
        console.log(this.obj,'111');
        console.log(this.obj.length);
        // this.obj.push({title:this.item.value,passage:this.item.address,input:this.txt_input});
    },
    sign_out() {
      this.input_ = false;
      this.btn_ = true;
    },
      querySearch(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      handleSelect(item) {
        console.log(item);
        this.item = item;
      },
      listenerFunction(e) {
      document.addEventListener("scroll", this.handleScroll, true);
      },
      beforeDestroy() {
        document.removeEventListener("scroll", this.listenerFunction);
      },
      handleScroll() {
        // handleScroll 和 methods 是同级
        if (window.pageYOffset > 400) {
          //window.pageYOffset:获取滚动距离
          this.isShow = true;
        } else {
          this.isShow = false;
        }
      },
      // 返回顶部
      backTop(){
        let top = document.documentElement.scrollTop || document.body.scrollTop;
        // 实现滚动效果
        const timeTop = setInterval(() => {
          document.body.scrollTop = document.documentElement.scrollTop = top -= 50;
          if (top <= 0) {
            clearInterval(timeTop);
          }
        }, 10);
      },

      // goBottom(){
      //   let top = document.documentElement.scrollTop || document.body.scrollTop;
      //   let top2 = 0;
      //   console.log(top,'222');
      //   const timeBottom = setInterval(() => {
      //     document.body.scrollTop = document.documentElement.scrollTop = top += 10;
      //     if (top == top2) {
      //       clearInterval(timeBottom);
      //     }
      //     let top = top2;
      //   }, 10);
      // },
      
    loadAll() {
        return [
          { "value": "三全鲜食（北新泾店）", "address": "长宁区新渔路144号" },
          { "value": "Hot honey 首尔炸鸡（仙霞路）", "address": "上海市长宁区淞虹路661号" },
          { "value": "新旺角茶餐厅", "address": "上海市普陀区真北路988号创邑金沙谷6号楼113" },
          { "value": "泷千家(天山西路店)", "address": "天山西路438号" },
          { "value": "胖仙女纸杯蛋糕（上海凌空店）", "address": "上海市长宁区金钟路968号1幢18号楼一层商铺18-101" },
          { "value": "贡茶", "address": "上海市长宁区金钟路633号" },
          { "value": "豪大大香鸡排超级奶爸", "address": "上海市嘉定区曹安公路曹安路1685号" },
          { "value": "茶芝兰（奶茶，手抓饼）", "address": "上海市普陀区同普路1435号" },
          { "value": "十二泷町", "address": "上海市北翟路1444弄81号B幢-107" },
          { "value": "星移浓缩咖啡", "address": "上海市嘉定区新郁路817号" },
          { "value": "阿姨奶茶/豪大大", "address": "嘉定区曹安路1611号" },
          { "value": "新麦甜四季甜品炸鸡", "address": "嘉定区曹安公路2383弄55号" },
          { "value": "Monica摩托主题咖啡店", "address": "嘉定区江桥镇曹安公路2409号1F，2383弄62号1F" },
          { "value": "浮生若茶（凌空soho店）", "address": "上海长宁区金钟路968号9号楼地下一层" },
          { "value": "NONO JUICE  鲜榨果汁", "address": "上海市长宁区天山西路119号" },
          { "value": "CoCo都可(北新泾店）", "address": "上海市长宁区仙霞西路" },
          { "value": "快乐柠檬（神州智慧店）", "address": "上海市长宁区天山西路567号1层R117号店铺" },
          { "value": "Merci Paul cafe", "address": "上海市普陀区光复西路丹巴路28弄6号楼819" },
          { "value": "猫山王（西郊百联店）", "address": "上海市长宁区仙霞西路88号第一层G05-F01-1-306" },
          { "value": "枪会山", "address": "上海市普陀区棕榈路" },
          { "value": "纵食", "address": "元丰天山花园(东门) 双流路267号" },
          { "value": "钱记", "address": "上海市长宁区天山西路" },
          { "value": "壹杯加", "address": "上海市长宁区通协路" },
          { "value": "唦哇嘀咖", "address": "上海市长宁区新泾镇金钟路999号2幢（B幢）第01层第1-02A单元" },
          { "value": "爱茜茜里(西郊百联)", "address": "长宁区仙霞西路88号1305室" },
          { "value": "爱茜茜里(近铁广场)", "address": "上海市普陀区真北路818号近铁城市广场北区地下二楼N-B2-O2-C商铺" },
          { "value": "鲜果榨汁（金沙江路和美广店）", "address": "普陀区金沙江路2239号金沙和美广场B1-10-6" },
          { "value": "开心丽果（缤谷店）", "address": "上海市长宁区威宁路天山路341号" },
          { "value": "超级鸡车（丰庄路店）", "address": "上海市嘉定区丰庄路240号" },
          { "value": "妙生活果园（北新泾店）", "address": "长宁区新渔路144号" },
          { "value": "香宜度麻辣香锅", "address": "长宁区淞虹路148号" },
          { "value": "凡仔汉堡（老真北路店）", "address": "上海市普陀区老真北路160号" },
          { "value": "港式小铺", "address": "上海市长宁区金钟路968号15楼15-105室" },
          { "value": "蜀香源麻辣香锅（剑河路店）", "address": "剑河路443-1" },
          { "value": "北京饺子馆", "address": "长宁区北新泾街道天山西路490-1号" },
          { "value": "饭典*新简餐（凌空SOHO店）", "address": "上海市长宁区金钟路968号9号楼地下一层9-83室" },
          { "value": "焦耳·川式快餐（金钟路店）", "address": "上海市金钟路633号地下一层甲部" },
          { "value": "动力鸡车", "address": "长宁区仙霞西路299弄3号101B" },
          { "value": "浏阳蒸菜", "address": "天山西路430号" },
          { "value": "四海游龙（天山西路店）", "address": "上海市长宁区天山西路" },
          { "value": "樱花食堂（凌空店）", "address": "上海市长宁区金钟路968号15楼15-105室" },
          { "value": "壹分米客家传统调制米粉(天山店)", "address": "天山西路428号" },
          { "value": "福荣祥烧腊（平溪路店）", "address": "上海市长宁区协和路福泉路255弄57-73号" },
          { "value": "速记黄焖鸡米饭", "address": "上海市长宁区北新泾街道金钟路180号1层01号摊位" },
          { "value": "红辣椒麻辣烫", "address": "上海市长宁区天山西路492号" },
          { "value": "(小杨生煎)西郊百联餐厅", "address": "长宁区仙霞西路88号百联2楼" },
          { "value": "阳阳麻辣烫", "address": "天山西路389号" },
          { "value": "南拳妈妈龙虾盖浇饭", "address": "普陀区金沙江路1699号鑫乐惠美食广场A13" }
        ];
      },
  },
};
</script>